package model;

import java.awt.Graphics;
import java.awt.Image;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JPanel;

public class Panneau_carte extends JPanel 
{
	private bddConnection connection = new bddConnection("localhost", "transat", "root", "");
	private Image terre;
	private Image ville;
	private Image transat;
	
	private static final int resolution_depart = 6;
	private static final int height_applet = 601;
	private static final int width_applet = 401;
	private static final int intensite_zoom = 5;
	
	private Carte carte;
	private Course course;
	private Bateau bateau;
	
	private Integer resolution;
	
	public Panneau_carte(Integer resolution)
	{
		this.resolution = resolution;
	}
	
	public void genererCarte(int id, Graphics g, int echelle, ResultSet resultat, int depart_x, int depart_y, ResultSet bateaux)
	{
		try
		{
			while(resultat.next())
			{
				int x = (resultat.getInt("position_x")-(depart_x-((height_applet/echelle)/2)))*echelle;
				int y = (resultat.getInt("position_y")-(depart_y-((width_applet/echelle)/2)))*echelle;
				g.drawImage(this.terre, x, y, echelle, echelle,this);
			}
			
			// Dessine les villes
			int debut_x = (this.carte.getCoordonneesById(this.carte.getVilleCoordonneesId(this.carte.ville_depart_id))[0]-(depart_x-((height_applet/echelle)/2)))*echelle;
			int debut_y = (this.carte.getCoordonneesById(this.carte.getVilleCoordonneesId(this.carte.ville_depart_id))[1]-(depart_y-((width_applet/echelle)/2)))*echelle;
			g.drawImage(this.ville, debut_x, debut_y, echelle, echelle,this);	
			
			int arrivee_x = (this.carte.getCoordonneesById(this.carte.getVilleCoordonneesId(this.carte.ville_arrivee_id))[0]-(depart_x-((height_applet/echelle)/2))*echelle);
			int arrivee_y = (this.carte.getCoordonneesById(this.carte.getVilleCoordonneesId(this.carte.ville_arrivee_id))[1]-(depart_y-((width_applet/echelle)/2))*echelle);
			g.drawImage(this.ville, arrivee_x, arrivee_y, echelle, echelle,this);	
			
			while(bateaux.next())
			{
				int x = (bateaux.getInt("position_x")-(depart_x-((height_applet/echelle)/2)))*echelle;
				int y = (bateaux.getInt("position_y")-(depart_y-((width_applet/echelle)/2)))*echelle;
				g.drawImage(this.transat, x, y, echelle, echelle,this);
			}
			
		}
		catch (Exception e)
		{
			System.out.println("echec pilote : "+e);
		}
	}
	
	public void surfaceOptimal(Graphics g, int resolution)
	{
		try
		{
			int depart_x = this.bateau.getPosition_x();
			int depart_y = this.bateau.getPosition_y();
			
			ResultSet resultat = this.carte.getAllCoordonnee(depart_x, depart_y, ((height_applet-1)/resolution)/2, ((width_applet-1)/resolution)/2);
			ResultSet bateaux = this.course.getAllBateau();
			
			this.genererCarte(this.carte.carte_id, g, resolution, resultat, depart_x, depart_y, bateaux);
		}
		catch (Exception e)
		{
			System.out.println("echec pilote : "+e);
		}
	}
	
	public void zoomer()
	{
		if(this.resolution < 50)
		{
			this.resolution+=intensite_zoom;
			repaint();
		}
		
	}
	
	public void dezoomer()
	{
		if(this.resolution > 1)
		{
			this.resolution-=intensite_zoom;
			repaint();
		}
	}
	
	public void paintComponent(Graphics g) 
	{
		try {
			this.carte = new Carte(1, new bddConnection("localhost", "transat", "root", ""));
			this.course = new Course(1, new bddConnection("localhost", "transat", "root", ""));
			this.bateau = new Bateau(2, new bddConnection("localhost", "transat", "root", ""));
		} catch (SQLException e) {
			System.out.println(e);
		}
		super.paintComponent(g);
		this.terre = getToolkit().getImage("../public/image/terre.png");
		this.ville = getToolkit().getImage("../public/image/ville.png");
		this.transat = getToolkit().getImage("../public/image/transat.png");
		if(this.resolution == null)
			this.resolution = resolution_depart;
		this.surfaceOptimal(g, resolution);
	}

}
